package com.android.sunflower.ui.plantlist

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.android.sunflower.R
import com.android.sunflower.databinding.PlantListItemBinding
import com.android.sunflower.logic.room.entities.Flower
import com.android.sunflower.ui.MainActivity

class PlantListAdapter(val plantList:List<Flower>):RecyclerView.Adapter<PlantListAdapter.ViewHolder>() {
    inner class ViewHolder(binding:PlantListItemBinding):RecyclerView.ViewHolder(binding.root){
        val binding=binding
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val plantListItemBinding=PlantListItemBinding.inflate(LayoutInflater.from(MainActivity.runningInstance),parent,false)
        return ViewHolder(plantListItemBinding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val navController=MainActivity.navController
        holder.itemView.setOnClickListener {
            val bundle=Bundle()
            bundle.putInt("position",position)
            navController?.navigate(R.id.action_plantListFragment_to_flowerInfo,bundle)
        }
        val flowerPic=holder.binding.plantPic
        val flowerName=holder.binding.plantName
        flowerPic.setImageResource(plantList[position].picture!!)
        flowerName.text=plantList[position].name
    }

    override fun getItemCount(): Int {
        return plantList.size
    }
}